package com.wink.service;

import com.google.code.morphia.query.Query;
import com.google.gson.Gson;
import com.mongodb.MongoException;
import com.storage.memory.FakeData;
import com.storage.memory.StorageMemory;
import com.storage.mongo.DBInterface;
import com.storage.mongo.ETopic;
import com.storage.mongo.Echarity;
import com.wink.charity.Charity;
import com.wink.charity.CharityList;
import com.wink.common.Identity;
import com.wink.dt.Comment;
import com.wink.dt.Topic;
import com.wink.dt.TopicList;
import com.wink.event.Event;
import com.wink.event.EventList;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.apache.wink.common.model.synd.SyndContent;
import org.apache.wink.common.model.synd.SyndEntry;
import org.apache.wink.common.model.synd.SyndText;
import org.apache.wink.server.utils.LinkBuilders;

public class CommunityServicesImpl implements CommunityServices {

	// POS singleton (better solution should be
	// provided!)
	private static CommunityServicesImpl svc;
	
	// simulate persistent storage (singleton svc ensures only one copy)
	private StorageMemory storage = new StorageMemory();
	private Topic topic = new Topic();
	

	public synchronized static CommunityServicesImpl getInstance() {
		if (svc == null) {
			System.out.println("---> SVR: creating demo data");

			// TODO here we should add the storage the service 
			svc = new CommunityServicesImpl();

			// we need data for demonstration	
			List<Charity> cs = FakeData.generateCharities();
			for (Charity c : cs)
				svc.storage.add(c);
			
			List<Event> es = FakeData.generateEvents();
			for (Event e : es)
				svc.storage.add(e);
			
			List<Topic> ts = FakeData.generateTopics();
			for (Topic t : ts)
				svc.storage.add(t);
		}

		return svc;
	}	
	
	@Override
	public String getServices() {	
		System.out.println("---> SVR: In getServices ");
		String[] services =  {"DiscussionThreads", "Events"};
		ToConverter obj = new ToConverter(services);		
		Gson gson = new Gson();
		String json = gson.toJson(obj);		
		return json;
	}
		
	//Charity actions
	@Override
	public String listAllCharity() throws UnknownHostException, MongoException {
		DBInterface di = new DBInterface();
		
		System.out.println("---> SVR: In listAllCharity ");
		
		List<Echarity> list = di.listCharity();
		ArrayList list1 = new ArrayList();
		list1.add(list);
		CharityList cList = new CharityList(list1);

		return CharityList.toJson(cList);
	}			
	
	@Override
	public String createCharity(String json) throws UnknownHostException, MongoException  {
		DBInterface di = new DBInterface();
		Charity charity = Charity.fromJson(json);
		System.out.println("---> SVR: In createCharity");
		System.out.println("---> SVR: createCharity --> "
				+ ", Charity Name" + charity.getName() 
				+ ", Charity Description" + charity.getDescription());
		
		di.createCharity(charity.getName(), charity.getDescription(), charity.getIdentity().getFromUser(), charity.getIdentity().getFromCommunity());
		return Charity.toJson(charity);
	}
	
	@Override
	public String GetCharityByName(@PathParam("name") String name ) throws UnknownHostException, MongoException {
		DBInterface di = new DBInterface();
		Charity charity = new Charity();
		System.out.println("---> SVR: In GetCharityByName");
		System.out.println("---> SVR: In parm " + name);
		Query<Echarity> query = di.getCharityDetails(name);
		
		
		for(Echarity c: query)
		{
			charity.setName(c.getCharityName());
			charity.setDescription(c.getCharityDescription());
			charity.setCharityCommunityID(c.getCommunityID());
			
		}
		
		return charity.toJson(charity);
	}
		
	@Override
	public String donate(String charity) throws NumberFormatException, UnknownHostException, MongoException {	
		DBInterface di = new DBInterface();
		System.out.println("---> SVR: In donate"  );
		Charity c = Charity.fromJson(charity);
		System.out.println("---> SVR: Donate to  --> "
				+ ", Charity Name " + c.getName() 
				+ ", amt " + c.getAmount());
		
		di.donateCharity(c.getName(), Integer.parseInt(c.getAmount()), c.getIdentity().getFromUser(), c.getIdentity().getFromCommunity());
		return Charity.toJson(c);
	}	
	
	//DT actions
	@Override
	public String listAllDiscussion() throws UnknownHostException, MongoException {
		DBInterface di = new DBInterface();
		ArrayList<Topic> list = new ArrayList<Topic>();
		ArrayList list1 = new ArrayList();
		System.out.println("---> SVR: In listAllDiscussion");
		Query<com.storage.mongo.ETopic> query = di.getTopics();		
		for(ETopic t : query)
		{
			list1.add(t.getTopicTitle());
		}
		list = list1;
		TopicList tList = new TopicList(list);
		System.out.println("Topic list crossed");
		return TopicList.toJson(tList);
	}
	
	@Override
	public String createTopic(String json) throws UnknownHostException, MongoException {
		DBInterface di = new DBInterface();
		Topic topic = Topic.fromJson(json);
		System.out.println("---> SVR: In createTopic");
		System.out.println("---> SVR: createTopic --> "
				+ ", Charity Name" + topic.getTopicTitle() 
				+ ", Charity Description" + topic.getTopicdescription());
		
		di.createTopic(topic.getTopicTitle(),topic.getTopicdescription(),topic.getIdentity().getFromUser(),topic.getIdentity().getFromCommunity());
		
		return Topic.toJson(topic);
	}
	
	@Override
	public void createComment( String json) throws UnknownHostException, MongoException {	
		DBInterface di = new DBInterface();
		Comment comment = Comment.fromJson(json);
		System.out.println("---> SVR: In createComment");
		System.out.println("---> SVR: createComment --> "
				+ ", CommentTopic Name" + comment.getTopicName() 
				+ ", Comment Description" + comment.getComment());

		di.addComments(comment.getTopicName(),comment.getIdentity().getFromUser(),comment.getComment());

	}		

	@Override
	public String getTopicByName(@PathParam("name") String name ) throws UnknownHostException, MongoException {
		DBInterface di = new DBInterface();
		//Topic topic = Topic.fromJson(json);
		System.out.println("---> SVR: In getTopicByName, " + name);		
		Topic topic = new Topic();
		Comment comment=new Comment();
		Identity identity=new Identity();
		ArrayList<Topic> list = new ArrayList<Topic>();
		ArrayList list1 = new ArrayList();
		Query<com.storage.mongo.ETopic> query = di.getComments(name);
		for(ETopic t : query)
		{
			System.out.println("comment in loop " + t.getComments().getCreatedText().toString());
			comment.setComment(t.getComments().getCreatedText().toString());
			identity.setFromUser(t.getComments().getCreatedBy().toString());
			comment.setIdentity(identity);
			topic.addToCommentList(comment);
			topic.setTopicdescription(t.getTopicContent());
		}
		
		return Topic.toJson(topic);
		
	}		
	
	@Override
	public String getTopicDetails(@PathParam("name") String name ) throws UnknownHostException, MongoException {
		DBInterface di = new DBInterface();
		System.out.println("---> SVR: In getTopicDetails, " + name);	

		Topic topic = new Topic();
		Comment comment=new Comment();
		Identity identity=new Identity();
		ArrayList<Topic> list = new ArrayList<Topic>();
		ArrayList list1 = new ArrayList();
		Query<com.storage.mongo.ETopic> query = di.getComments(name);
		for(ETopic t : query)
		{
			System.out.println("comment in loop " + t.getComments().getCreatedText().toString());
			comment.setComment(t.getComments().getCreatedText().toString());
			identity.setFromUser(t.getComments().getCreatedBy().toString());
			comment.setIdentity(identity);
			topic.addToCommentList(comment);
			topic.setTopicdescription(t.getTopicContent());
		}
		
		return Topic.toJson(topic);
		
	}
	
	public static void main(String[] args) {
		//new CommunityServicesImpl().getServices();
	}
	
	
}

class ToConverter
{
	public String service1 ,service2;
	
   public ToConverter(String[] convert)
   {
	   service1 = convert [0];
	   service2 = convert [1];	   
   }
}
